package com.dhcc.bpm.modules.scf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.utils.StringUtils;
import com.dhcc.bpm.modules.scf.dto.ScfRecyclingWeighInfoDTO;
import com.dhcc.bpm.modules.scf.entity.ScfRecyclingWeighInfo;
import com.dhcc.bpm.modules.scf.service.IScfRecyclingWeighInfoService;
import com.dhcc.bpm.modules.scf.vo.ScfRecyclingWeighInfoVO.ScfGetRecyclingWeighDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfRecyclingWeighInfoVO.ScfGetRecyclingWeighListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 利废企业过磅控制层
 */
@RestController
@AllArgsConstructor
public class ScfRecyclingWeighInfoController extends BladeController {

    private IScfRecyclingWeighInfoService scfRecyclingWeighInfoService;

    /**
     * 查询利废企业磅单列表
     *
     * @param scfRecyclingWeighInfo
     * @param query
     * @return
     */
    @GetMapping("/scfGetRecyclingWeighList")
    // @Cacheable(value = "scfWeighListCache", key = "'scfWeighListCacheKey-' + #scfWeighInfo.hashCode() + '-' + #query.hashCode()", unless = "#result == null")
    public R<IPage<ScfGetRecyclingWeighListVO>> pageList(ScfRecyclingWeighInfoDTO scfRecyclingWeighInfo, Query query) {
        if (StringUtils.isEmpty( scfRecyclingWeighInfo.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }
        scfRecyclingWeighInfo.setOrgCodes(  scfRecyclingWeighInfo.getOrgCode().split(",") );


        if(scfRecyclingWeighInfo.getOrgCode().contains( "50000000")){
            scfRecyclingWeighInfo.setOrgCode("50000000");
            scfRecyclingWeighInfo.setOrgCodes("50000000".split(",") );
        }

        return R.data(scfRecyclingWeighInfoService.selectScfRecyclingWeighInfoPage(Condition.getPage(query), scfRecyclingWeighInfo));
    }

    /**
     * 查询利废企业磅单详情
     *
     * @param scfRecyclingWeighInfo
     * @return
     */
    @GetMapping("/scfGetRecyclingWeighDetails")
    // @Cacheable(value = "scfWeighDetailsCache", key = "'scfWeighDetailsCacheKey-' + #scfWeighInfo.rowId")
    public R<ScfGetRecyclingWeighDetailsVO> details(ScfRecyclingWeighInfoDTO scfRecyclingWeighInfo) {
        return R.data(scfRecyclingWeighInfoService.getDetail(scfRecyclingWeighInfo));
    }


    /**
     * 新增利废企业磅单信息
     *
     * @param scfRecyclingWeighInfo
     * @return
     */
    @PostMapping("scfAddRecyclingWeighDetails")
    // @CacheEvict(value = "scfWeighListCache", allEntries = true)
    public R<Boolean> saveWeigh(@RequestBody ScfRecyclingWeighInfoDTO scfRecyclingWeighInfo) throws Exception {
        return R.status(scfRecyclingWeighInfoService.saveRecyclingWeighInfo(scfRecyclingWeighInfo));
    }

    /**
     * 修改利废企业磅单信息
     *
     * @param scfRecyclingWeighInfo
     * @return
     */
    @PostMapping("/scfModRecyclingWeighDetails")
    // @CacheEvict(value = "scfWeighDetailsCache", key = "'scfWeighDetailsCacheKey-' + #scfWeighInfo.rowId")
    public R<Boolean> updateWeigh(@RequestBody ScfRecyclingWeighInfoDTO scfRecyclingWeighInfo) throws Exception {
        return R.status(scfRecyclingWeighInfoService.updateRecyclingWeighInfo(scfRecyclingWeighInfo));
    }

    /**
     * 批量导入利废企业过磅信息
     * 导入过磅单
     */
    @PostMapping("/scfRecyclingWeighFile")
    // @CacheEvict(value = "scfFileInfoListCache", allEntries = true)
    public R<Boolean> uploadFile(@RequestParam("operatorId") String operatorId, MultipartFile file) throws Exception {
        // 获取到批量导入的数据集合
        List<ScfRecyclingWeighInfo> scfRecyclingWeighInfos = scfRecyclingWeighInfoService.batchSaveRecyclingWeighInfo(operatorId, file);
        int bachSaveRecyclingWeighInfo = scfRecyclingWeighInfoService.bachSaveRecyclingWeighInfo(scfRecyclingWeighInfos, operatorId);
        int total = scfRecyclingWeighInfos.size();
        int totalSussess = bachSaveRecyclingWeighInfo;
        int totalFail = total - totalSussess;
        String msg = "解析到" + total + "条数据，" + "导入成功" + totalSussess + "条，导入失败" + totalFail + "条！";
        return R.success(msg);
    }

    @PostMapping("/scfRecyclingWeighInfo/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {

        String[] split = ids.split(",");

        return R.status(scfRecyclingWeighInfoService.deleteByIds( split ));
    }
}
